<template>
  <el-dialog
    title="积分使用详情"
    :visible.sync="dialogVisible"
    :show="show"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @open='open'
    @close="$emit('update:show',false)"
  >
    <el-scrollbar ref="myScrollbar" :wrap-class="{wrapClass: true}">
      <div class="el-dialog-div">
        <el-table
          v-loading="infoLoding"
          :data="list"
          element-loading-text="Loading"
          fit
          border
          size="medium"
          style="margin-bottom: 20px"
          :height="listQuery.total>0 ? 'calc(100vh - 400px)' : 'calc(100vh - 400px)'"
          :header-cell-style="{background: '#F2F6FC'}"
        >
          <el-table-column align="center" fixed="left" label="序号" width="50" type="index">
          </el-table-column>
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-form label-position="left" label-width="100px">
                <el-form-item label="营销任务名称:" >
                  <span>{{ getInformation(scope.row.ext,"EmailMarketingName") }}</span>
                </el-form-item>
                <el-form-item label="营销任务时间:" >
                  <span>{{ getInformation(scope.row.ext,"TimingTransmission") }}</span>
                </el-form-item>
                <el-form-item label="收件人:">
                  <span v-html="getInformation(scope.row.ext,'Addressee')"></span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column align="center" label="用户名" min-width="80px">
            <template slot-scope="scope">
              {{ scope.row.operateUser }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="积分类型" min-width="80px">
            <template v-for="item in consumptionTypeOptions" slot-scope="scope">
              <template v-if="item['key'] === scope.row.consumptionType">
                {{ item.label }}
              </template>
            </template>
          </el-table-column>
          <el-table-column align="center" label="积分数量" min-width="80px">
            <template slot-scope="scope">
              {{ scope.row.pointsNum }}
            </template>
          </el-table-column>
          <el-table-column label="扣除时间" width="155px" align="center">
            <template slot-scope="scope">
              {{ scope.row.createdAt }}
            </template>
          </el-table-column>
        </el-table>
        <pagination
          style="margin-bottom: 15px;"
          v-show="listQuery.total>0"
          :total="listQuery.total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.limit"
          @pagination="open"
        />
      </div>
    </el-scrollbar>
  </el-dialog>
</template>

<style scoped>
/deep/ .el-dialog {
  min-width: 470px;
  max-width: 1000px;
}

/deep/ .el-dialog__body {
  padding: 0;
}

.el-dialog-div {
  padding: 20px 20px 0 20px;
  min-width: 400px;
}
</style>
<script>
import Pagination from '@/components/Pagination'
import { getAction } from '@/api/common'
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    companyId: {
      type: [Number, String],
      default: ''
    }
  },
  data() {
    return {
      list: null,
      listQuery: {
        page: 1,
        total: 0,
        limit: 10,
        id: ''
      },
      dialogVisible: this.show,
      infoLoding: true,
      consumptionTypeOptions: [
        { key: 1, label: '服务器发送邮件' }
      ]
    }
  },
  components: {
    Pagination
  },
  methods: {
    async open() {
      this.infoLoding = true
      this.listQuery.id = this.companyId
      const { data } = await getAction(this.listQuery, 'PointsConsumptionLog/GetList')
      this.list = data.items
      this.listQuery.total = data.total
      this.infoLoding = false
    },
    getInformation(ext, option) {
      // console.log(ext, option)
      const res = JSON.parse(ext)
      if (res === null) {
        return '暂无'
      }
      switch (option) {
        case 'EmailMarketingName':
          return res.EmailMarketingName
        case 'TimingTransmission':
          return res.TimingTransmission
        case 'Addressee':
          var str = ''
          for (const i in res.Addressee) {
            str = str + '<span>' + '邮箱： ' + res.Addressee[i].Addressee + '　　　' + '客户名称： ' + res.Addressee[i].Name + '   ' + '</span>' + '<br>'
          }
          return str
      }
    }
  },
  watch: {
    show() {
      this.dialogVisible = this.show
    }
  }
}
</script>
